package com.smugmugsoftware.airconcalc.advanced;

import java.io.Serializable;
import java.util.Date;
import java.util.Vector;

import android.os.Parcel;
import android.os.Parcelable;

public abstract class RoomComponent implements Comparable<RoomComponent>, Parcelable, Serializable {

	private static final long serialVersionUID = 1000L;

	protected long id;
	protected int lengthMajorUnit;
	protected int lengthMinorUnit;
	protected int widthMajorUnit;
	protected int widthMinorUnit;
	protected double area;
	protected int facing;
	protected String constructionNumber;
	protected boolean ismetric;
	protected boolean useArea;

	public RoomComponent(long id) {
		this.id = id;
		
		lengthMajorUnit = 0;
		lengthMinorUnit = 0;
		widthMajorUnit = 0;
		widthMinorUnit = 0;
		area = 0;
		facing = 0;
		constructionNumber = "";
		ismetric = false;
		useArea = false;
	}

	public long getID() {
		return id;
	}

	public void setID(long id) {
		this.id = id;
	}

	public String getLengthString() {
		String returnString = new String();
		if (lengthMajorUnit > 0) {
			returnString = String.valueOf(lengthMajorUnit) + "'";
		}

		returnString = returnString + String.valueOf(lengthMinorUnit) + "\"";
		return returnString;
	}

	public String getWidthString() {
		String returnString = new String();
		if (widthMajorUnit > 0) {
			returnString = String.valueOf(widthMajorUnit) + "'";
		}

		returnString = returnString + String.valueOf(widthMinorUnit) + "\"";
		return returnString;
	}

	public void setDimensions(int lengthMajorUnit, int lengthMinorUnit, int widthMajorUnit, int widthMinorUnit) {
		this.lengthMajorUnit = lengthMajorUnit;
		this.lengthMinorUnit = lengthMinorUnit;
		this.widthMajorUnit = widthMajorUnit;
		this.widthMinorUnit = widthMinorUnit;
		this.useArea = false;
	}

	public void setDimensions(int[] dims) {
		setDimensions(dims[0], dims[1], dims[2], dims[3]);
	}

	public double getArea() {
		if (useArea) {
			return area;
		} else {
			return (getTotalLength() * getTotalWidth());
		}
	}

	public String getAreaString() {
		return String.valueOf((int) getArea());
	}

	public double getTotalLength() {
		return (lengthMajorUnit + (lengthMinorUnit / 12));
	}
	public double getTotalWidth() {
		return (widthMajorUnit + (widthMinorUnit / 12));
	}
	public void setArea(double area) {
		this.area = area;
		this.useArea = true;
	}
	public int getFacing() {
		return facing;
	}
	public double getLengthMajorUnit() {
		return lengthMajorUnit;
	}

	public double getLengthMinorUnit() {
		return lengthMinorUnit;
	}

	public double getWidthMajorUnit() {
		return widthMajorUnit;
	}

	public double getWidthMinorUnit() {
		return widthMinorUnit;
	}

	public void setFacing(int facing) {
		this.facing = facing;
	}
	public void setFacing(String facing) {
		if (facing.equals("N")) {
			this.facing = 0;
		} else if (facing.equals("NE")) {
			this.facing = 1;
		} else if (facing.equals("E")) {
			this.facing = 2;
		} else if (facing.equals("SE")) {
			this.facing = 3;
		} else if (facing.equals("S")) {
			this.facing = 4;
		} else if (facing.equals("SW")) {
			this.facing = 5;
		} else if (facing.equals("W")) {
			this.facing = 6;
		} else if (facing.equals("NW")) {
			this.facing = 7;
		}
	}
	public String getFacingString() {
		if (facing == 0) {
			return "N";
		} else if (facing == 1) {
			return "NE";
		} else if (facing == 2) {
			return "E";
		} else if (facing == 3) {
			return "SE";
		} else if (facing == 4) {
			return "S";
		} else if (facing == 5) {
			return "SW";
		} else if (facing == 6) {
			return "W";
		} else if (facing == 7) {
			return "NW";
		} else {
			this.facing = 0;
			return "N";
		}
	}

	public String getLongFacingString() {
		if (facing == 0) {
			return "North";
		} else if (facing == 1) {
			return "Northeast";
		} else if (facing == 2) {
			return "East";
		} else if (facing == 3) {
			return "Southeast";
		} else if (facing == 4) {
			return "South";
		} else if (facing == 5) {
			return "Southwest";
		} else if (facing == 6) {
			return "West";
		} else if (facing == 7) {
			return "Northwest";
		} else {
			this.facing = 0;
			return "North";
		}
	}
	public String getConstructionNumber() {
		return constructionNumber;
	}
	public void setConstructionNumber(String constructionNumber) {
		if (constructionNumber.indexOf("-") != -1) {
			this.constructionNumber = constructionNumber;
		} else {
			this.constructionNumber = constructionNumber.substring(0, 2) + "-" + constructionNumber.substring(2);
		}
	}

	public boolean usesArea() {
		return useArea;
	}

	public int compareTo(RoomComponent another) {
		if (this.id < another.getID())
			return -1;
		else if (this.id > another.getID())
			return 1;
		else
			return 0;
	}

	public int describeContents() { return 0; }

	public void writeToParcel(Parcel dest, int flags) {

		dest.writeLong(id);	
		dest.writeString(constructionNumber);	
		dest.writeBooleanArray(new boolean[] { ismetric, useArea });
		dest.writeDouble(area);
		dest.writeInt(lengthMajorUnit);
		dest.writeInt(lengthMinorUnit);
		dest.writeInt(widthMajorUnit);
		dest.writeInt(widthMinorUnit);
		dest.writeInt(facing);
	}

	public RoomComponent(Parcel p) {

		this.id = p.readLong();
		this.constructionNumber = p.readString();
		
		boolean[] booleanArray = new boolean[2];
		p.readBooleanArray(booleanArray);
		this.ismetric = booleanArray[0];
		this.useArea = booleanArray[1];
		
		this.area = p.readDouble();
		this.lengthMajorUnit = p.readInt();
		this.lengthMinorUnit = p.readInt();
		this.widthMajorUnit = p.readInt();
		this.widthMinorUnit = p.readInt();
		this.facing = p.readInt();
	}
}
